﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using System.Web;

namespace DTcms.Common
{
    /// <summary>
    /// 验证码
    /// </summary>
    public class VerifyHelper
    {
        public static string method_1(int int_3)
        {
            Encoding encoding = Encoding.GetEncoding("gb2312");
            object[] array = method_2(int_3);
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < int_3; i++)
            {
                string @string = encoding.GetString((byte[])Convert.ChangeType(array[i], typeof(byte[])));
                stringBuilder.Append(@string);
            }
            return stringBuilder.ToString();
        }
        public static object[] method_2(int int_3)
        {
            string[] array = new string[]
            {
            "0",
            "1",
            "2",
            "3",
            "4",
            "5",
            "6",
            "7",
            "8",
            "9",
            "a",
            "b",
            "c",
            "d",
            "e",
            "f"
            };
            Random random = new Random();
            object[] array2 = new object[int_3];
            for (int i = 0; i < int_3; i++)
            {
                int num = random.Next(11, 14);
                string str = array[num].Trim();
                random = new Random(num * (int)DateTime.Now.Ticks + i);
                int num2;
                if (num == 13)
                {
                    num2 = random.Next(0, 7);
                }
                else
                {
                    num2 = random.Next(0, 16);
                }
                string str2 = array[num2].Trim();
                random = new Random(num2 * (int)DateTime.Now.Ticks + i);
                int num3 = random.Next(10, 16);
                string str3 = array[num3].Trim();
                random = new Random(num3 * (int)DateTime.Now.Ticks + i);
                int num4;
                if (num3 == 10)
                {
                    num4 = random.Next(1, 16);
                }
                else if (num3 == 15)
                {
                    num4 = random.Next(0, 15);
                }
                else
                {
                    num4 = random.Next(0, 16);
                }
                string str4 = array[num4].Trim();
                byte b = Convert.ToByte(str + str2, 16);
                byte b2 = Convert.ToByte(str3 + str4, 16);
                byte[] value = new byte[]
                {
                    b,
                    b2
                };
                array2.SetValue(value, i);
            }
            return array2;
        }
        public static byte[] method_14(int int_3, string string_4)
        {
            if (!string.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]))
            {
                return method_15(int_3, string_4);
            }
            int num = 26 * int_3 + 10;
            int num2 = 40;
            Bitmap bitmap = new Bitmap(num, 40);
            Random random = new Random((int)DateTime.Now.Ticks);
            int num3 = random.Next(255) % 128 + 128;
            int num4 = random.Next(255) % 128 + 128;
            int num5 = random.Next(255) % 128 + 128;
            Graphics graphics = Graphics.FromImage(bitmap);
            graphics.FillRectangle(new SolidBrush(Color.FromArgb(num3, num4, num5)), 0, 0, num, 40);
            int num6 = 5;
            Pen pen = new Pen(Color.FromArgb(num3 - 17, num4 - 17, num5 - 17), 2f);
            for (int i = 0; i < num6; i++)
            {
                int x = random.Next() % num;
                int y = random.Next() % num2;
                int x2 = random.Next() % num;
                int y2 = random.Next() % num2;
                graphics.DrawLine(pen, x, y, x2, y2);
            }
            string text = string_4;
            string text2 = "";
            for (int j = 0; j < int_3; j++)
            {
                int num7 = j * 26 + random.Next(5);
                int num8 = random.Next(10) + 1;
                Font font = new Font("Arial", (float)(14 + random.Next() % 4), FontStyle.Bold);
                string text3 = text.Substring(j, 1);
                text2 += text3.ToString();
                graphics.DrawString(text3.ToString(), font, new SolidBrush(Color.FromArgb(num3 - 68, num4 - 68, num5 - 68)), (float)num7, (float)num8);
            }
            MemoryStream memoryStream = new MemoryStream();
            bitmap.Save(memoryStream, ImageFormat.Jpeg);
            bitmap.Dispose();
            graphics.Dispose();
            string_4 = text2;
            byte[] result = memoryStream.ToArray();
            memoryStream.Close();
            return result;
        }
        public static byte[] method_15(int int_3, string string_4)
        {
            Random random = new Random();
            int num = random.Next(40, 60);
            int num2 = random.Next(10, 20);
            int num3 = random.Next(25, 40);
            int num4 = random.Next(10, 40);
            int num5 = random.Next(30, 70);
            int num6 = random.Next(1, 10);
            random.Next(200, 500);
            if (num6 > 5)
            {
                num5 = -num5;
            }
            int num7 = num3 * int_3 + 10;
            int num8 = num;
            Bitmap bitmap = new Bitmap(num7, num8);
            Random random2 = new Random((int)DateTime.Now.Ticks);
            int num9 = random2.Next(255) % 128 + 128;
            int num10 = random2.Next(255) % 128 + 128;
            int num11 = random2.Next(255) % 128 + 128;
            Graphics graphics = Graphics.FromImage(bitmap);
            graphics.FillRectangle(new SolidBrush(Color.FromArgb(num9, num10, num11)), 0, 0, num7, num8);
            Pen pen = new Pen(Color.FromArgb(num9 - num2, num10 - num2, num11 - num2), 2f);
            for (int i = 0; i < num4; i++)
            {
                int x = random2.Next() % num7;
                int y = random2.Next() % num8;
                int x2 = random2.Next() % num7;
                int y2 = random2.Next() % num8;
                graphics.DrawLine(pen, x, y, x2, y2);
            }
            string text = string_4;
            string text2 = "";
            for (int j = 0; j < int_3; j++)
            {
                int num12 = j * 26 + random2.Next(5);
                int num13 = random2.Next(10) + 1;
                Font font = new Font("Arial", (float)(num2 + random2.Next() % 4), FontStyle.Bold);
                string text3 = text.Substring(j, 1);
                text2 += text3.ToString();
                graphics.DrawString(text3.ToString(), font, new SolidBrush(Color.FromArgb((num9 + num5 > 255) ? 255 : (num9 + num5), (num10 + num5 > 255) ? 255 : (num10 + num5), (num11 + num5 > 255) ? 255 : (num11 + num5))), (float)num12, (float)num13);
            }
            MemoryStream memoryStream = new MemoryStream();
            bitmap.Save(memoryStream, ImageFormat.Jpeg);
            bitmap.Dispose();
            graphics.Dispose();
            string_4 = text2;
            byte[] result = memoryStream.ToArray();
            memoryStream.Close();
            return result;
        }
    }
}
